package com.example.logindb.controller;



import com.example.logindb.dto.ResponseMessage;
import com.example.logindb.entity.Location;
import com.example.logindb.entity.Online;
import com.example.logindb.entity.SaL;
import com.example.logindb.entity.User;
import com.example.logindb.sevices.LocationService;
import com.example.logindb.sevices.OnlineService;
import com.example.logindb.sevices.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/location")
public class LocationController {
    @Autowired
    LocationService locationService;
    @Autowired
    OnlineService onlineService;
    @Autowired
    UserService userService;

    //更新在线位置
    @PostMapping
    public ResponseMessage<Location> updateStatus(@RequestBody Location location)
    {
        locationService.updateLocation(location);
        return ResponseMessage.success(location);
    }

    //查询在线位置
    @GetMapping("/{id}")
    public ResponseMessage<SaL> get_relation(@PathVariable Integer id)
    {
        User user = userService.getUser(id);
        if (user == null)
            return ResponseMessage.fail0(null);
        Online online = onlineService.findStatusById(id);
        Location location = locationService.findAllById(id);
        if(online == null|| location == null)
        {
            SaL saL = new SaL(0, 0, 0);
            return ResponseMessage.fail(saL);
        }
        SaL saL = new SaL(location.getLat(), location.getLat(), online.getStatus());
        return ResponseMessage.success(saL);
    }

}
